/*************************************************************************
 * File Name:    Implement_strStr.cc
 * Author:       zero91
 * Mail:         jianzhang9102@gmail.com
 * Created Time: 2013-11-15 14:08:44
 * 
 * Description:  
 |------------------------------------------------------------------------
 | Problem: Implement strStr()
 |
 | Implement strStr()
 | Returns a pointer to the first occurrence of needle in haystack,
 | or null if needle is not part of haystack.
 |------------------------------------------------------------------------
 ************************************************************************/

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <map>
#include <set>
#include <functional>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>

using namespace std;

class Solution {
public:
    char *strStr(char *haystack, char *needle)
    {
        char *p, *q;
        
        while (*haystack != '\0') {
            for (p = haystack, q = needle; *p != '\0' && *q != '\0'; ++p, ++q) {
                if (*p != *q) break; 
            }
            if (*q == '\0') return haystack;
            if (*p == '\0') return NULL;
            ++haystack;
        }
        return *needle == '\0' ? haystack : NULL;
    }
};

